//
//  JYListController.m
//  JYShop
//
//  Created by 花花 on 2017/9/28.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYListController.h"
#import "JYSearchBar.h"
#import "JYSearchViewController.h"
#import "JYFunView.h"
#import "JYFunModel.h"
#import <MJRefresh/MJRefresh.h>

#import "JYPublicCell.h"
#import "JYShopCell.h"
#import "JYFontColorTool.h"
#import "JYListModel.h"

@interface JYListController ()<UITableViewDataSource,UITableViewDelegate>

//搜索
@property(nonatomic) JYSearchBar *searchBar;

//collectionView 新鲜蔬菜 肉禽蛋类 时令水果 酒水饮料 水产冻货
@property (nonatomic, strong) JYFunView *funView;

@property (nonatomic, strong) UIScrollView *scrollView;

@property(nonatomic,strong)NSMutableArray *funArr;

@property(nonatomic,strong)NSMutableArray *dataArr;

@property (nonatomic) UITableView *tableView;

@end

static NSString  *const ListCellID = @"ListCellID";
@implementation JYListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self loadDataFromNetwork];
    [self setupTableView];
    [self setupSubView];
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}


- (void)loadDataFromNetwork{

    [HTTPRequest POST:kCommonUserGreensListUrl parameter:@{@"userId":@"52891c9c-9f58-450c-9027-d2e087573f3a"} success:^(id resposeObject) {
       
        if (SUCCESS) {
        
            for (NSDictionary *dic in resposeObject[@"bean"]) {
                
                
                JYListModel *model = [JYListModel mj_objectWithKeyValues:dic];
               
                [self.dataArr addObject:model];
            }
            [self.tableView reloadData];
            
        }else{
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
    }];


}
- (void)setupSubView{
    [self.view addSubview:self.scrollView ];

    JYWeakSelf;
    
    _searchBar = [[JYSearchBar alloc]initWithOffSet:10];
    _searchBar.backgroundColor = [UIColor whiteColor];
    _searchBar.searchBarClickCallBack = ^{
        JYSearchViewController *searchVc = [JYSearchViewController new];
        
        [weakSelf.navigationController pushViewController:searchVc animated:YES];
    };
    [self.view addSubview:_searchBar];
    
    [_searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view.mas_left);
        make.right.equalTo(weakSelf.view.mas_right);
        make.top.equalTo(weakSelf.view.mas_top);
        make.height.equalTo(@64);
    }];
    
    [self.scrollView addSubview:self.funView];
    [self.scrollView addSubview:self.tableView];
    
    
}

- (void)setupTableView{
    
    self.tableView.rowHeight = 84;
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JYPublicCell *cell = [tableView dequeueReusableCellWithIdentifier:ListCellID];
    cell.listModel = [self.dataArr safeObjectAtIndex:indexPath.row];
    return cell;
    
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    for (UIView * sv in cell.subviews) {
        
        if ([sv isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            
            NSLog(@"删除%zd",indexPath.row);
        }
    }
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.scrollView) {
    
        _searchBar.progress = scrollView.contentOffset.y/64;
//        CGFloat contentOffsetY = scrollView.contentOffset.y;
//        
//        if (contentOffsetY >= bannerHight-44) {
//            
//            [UIView animateWithDuration:0.2 animations:^{
//                self.scrollView.contentOffset = CGPointMake(0, bannerHight-44);
//            }];
//        }
//        
    }
//    if ([scrollView isKindOfClass:[UITableView class]]) {
//        
//        CGFloat contentOffsetY = scrollView.contentOffset.y;
//        
//        if (contentOffsetY <= 0) {
//            [UIView animateWithDuration:0.5 animations:^{
//                self.scrollView.contentOffset = CGPointMake(0, 0);
//            }];
//            
//            //            [self.scrollView scrollRectToVisible:CGRectMake(0, 0, self.scrollView.width , self.scrollView.height) animated:YES];
//            
//        } else {
//            
//            [UIView animateWithDuration:0.5 animations:^{
//                self.scrollView.contentOffset = CGPointMake(0, bannerHight-44);
//            }];
//        }
//        
//    }
}

- (UIScrollView *)scrollView{
    
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, Screen_Width, Screen_Height-49-20)];
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        _scrollView.contentSize = CGSizeMake(Screen_Width, scrollViewContentHeight- bannerHight);
        //        _scrollView.backgroundColor = [UIColor redColor];
        
        
    }
    return _scrollView;
}

- (JYFunView *)funView{
    
    if (!_funView) {
        _funView = [[JYFunView alloc]initWithFrame:CGRectMake(0,45, Screen_Width, funHight)];
        _funView.dataArr = self.funArr;
    }
    
    return _funView;
}
- (NSMutableArray *)funArr{
    
    if (!_funArr) {
        _funArr = [NSMutableArray array];
        
        NSArray *titleArr = @[@"新鲜蔬菜",@"肉禽蛋类",@"时令水果",@"酒水饮料",@"水产冻货",@"米面粮油",@"调味其他",@"南北干货",@"餐厨用品",@"预制菜"];
        NSArray *imgArr = @[@"home_icon_dry_cargo",@"home_icon_meat",@"home_icon_fruit",@"home_icon_wine",@"home_icon_fish",@"home_icon_meter",@"home_icon_seasoning",@"home_icon_dry_cargo",@"home_icon_kitchen",@"home_icon_dish"];
        
        for (int i =0; i< titleArr.count; i++) {
            
            JYFunModel *model  =[[JYFunModel alloc]init];
            model.title = titleArr[i];
            model.icon = imgArr[i];
            [_funArr addObject:model];
        }
    }
    return _funArr;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView =  [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.funView.frame)+10, Screen_Width,tableViewHeight)];

        _tableView.bounces = NO;
       [_tableView registerNib:[UINib nibWithNibName:@"JYPublicCell" bundle:nil] forCellReuseIdentifier:ListCellID];
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = YES;
        
    }
    return _tableView;
}

- (NSMutableArray *)dataArr{

    if (!_dataArr ) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;

}
@end
